// pages/index/index.js
var common = require('../../lib/common.js');
var url = require('../../config/url.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 轮播图
        slideshowData: [],
        // 菜单
        menuData: [],
        // 搜索词
        searchValue: "",
        // 选项
        options: {
            nav: [{
                id: 0,
                name: '最新发布'
            }, {
                id: 1,
                name: '平台发布'
            }, {
                id: 2,
                name: '热门发布'
            }, {
                id: 3,
                name: '平台推荐'
            }],
            active: 0,
            page: [1, 1, 1, 1],
            articles: [
                [],
                [],
                [],
                []
            ],
            loadding: [false, false, false, false],
        },
        // 通知
        notification: "",
        scrollTop: 0,
        lastTime: 0,
    },

    onShow() {
        if (this.data.lastTime != 0) {
            common.post(url.chunkArticleUpdated, { time: this.data.lastTime }, (data) => {
                if (data !== false) {
                    wx.startPullDownRefresh();
                }
            });
        }
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getMenus();
        this.getSlideshow();
        this.getNotification();
        this.loadArticle(0);
    },

    onPullDownRefresh() {
        this.setData({
            options: {
                nav: [{
                    id: 0,
                    name: '最新发布'
                }, {
                    id: 1,
                    name: '平台发布'
                }, {
                    id: 2,
                    name: '热门发布'
                }, {
                    id: 3,
                    name: '平台推荐'
                }],
                active: 0,
                page: [1, 1, 1, 1],
                articles: [
                    [],
                    [],
                    [],
                    []
                ],
                loadding: [false, false, false, false],
            }
        });
        this.setData();
        this.getMenus();
        this.getSlideshow();
        this.getNotification();
        this.loadArticle(0);
        wx.stopPullDownRefresh();
    },

    /**
     * 获取轮播图
     */
    getNotification() {
        common.post(url.getHomeNotification, {}, (data) => {
            this.setData({ notification: data });
        });
    },

    /**
     * 获取菜单
     */
    getMenus() {
        common.post(url.getOneType, {}, (data) => {
            for (let i in data) {
                data[i].url = '/pages/article_type/index?id=' + data[i].id + '&name=' + data[i].name;
            }
            this.setData({ menuData: data });
        });
    },
    /**
     * 获取轮播图
     */
    getSlideshow() {
        common.post(url.getSlideshow, {}, (data) => {
            this.setData({ slideshowData: data });
        });
    },
    /**
     * 获取最新列表信息
     */
    getNewArticle(index, complete) {
        common.post(url.getNewArticleList, { page: this.data.options.page[index] }, (data) => {
            for (let i in data) {
                data[i].url = '/pages/aritcle_detail/index?id=' + data[i].id
                if (data[i].images == null)
                    data[i].images = [];
                else {
                    data[i].images = (data[i].images + '').split(',');
                    data[i].images = data[i].images.map((n) => {
                        return n;
                    })
                }
            }
            if (data.length > 0 && this.data.options.page[index] == 1) {
                this.setData({ lastTime: data[0].updated });
            }
            if (complete)
                complete(data);
        });
    },

    /**
     * 获取最热推荐
     */
    getHotArticle(index, complete) {
        common.post(url.getHotArticle, { page: this.data.options.page[index] }, (data) => {
            for (let i in data) {
                data[i].url = '/pages/aritcle_detail/index?id=' + data[i].id
                if (data[i].images == null)
                    data[i].images = [];
                else {
                    data[i].images = (data[i].images + '').split(',');
                    data[i].images = data[i].images.map((n) => {
                        return n;
                    })
                }
            }
            if (complete)
                complete(data);
        });
    },

    /**
     * 获取平台推荐
     */
    getRecommendArticle(index, complete) {
        common.post(url.getRecommendArticle, { page: this.data.options.page[index] }, (data) => {
            for (let i in data) {
                data[i].url = '/pages/aritcle_detail/index?id=' + data[i].id
                if (data[i].images == null)
                    data[i].images = [];
                else {
                    data[i].images = (data[i].images + '').split(',');
                    data[i].images = data[i].images.map((n) => {
                        return n;
                    })
                }
            }
            if (complete)
                complete(data);
        });
    },


    /**
     * 获取平台发布
     */
    getTerraceRecommendArticle(index, complete) {
        common.post(url.getTerraceRecommend, { page: this.data.options.page[index] }, (data) => {
            for (let i in data) {
                data[i].url = '/pages/aritcle_detail/index?id=' + data[i].id
                if (data[i].images == null)
                    data[i].images = [];
                else {
                    data[i].images = (data[i].images + '').split(',');
                    data[i].images = data[i].images.map((n) => {
                        return n;
                    })
                }
            }
            if (complete)
                complete(data);
        });
    },


    /**
     * 搜索
     */
    onSearch(e) {
        // this.setData({optionScrollTop:});
        console.log(e);
    },

    /**
     * 选项
     */
    onOptionChange({ detail }) {
        let options = this.data.options;
        options.active = detail.index;
        this.setData({ options });

        if (options.page[detail.index] > 1 || options.page[detail.index] === false) {
            return;
        }
        this.loadArticle(detail.index);
    },
    onReachBottom() {
        let options = this.data.options;
        if (options.page[options.active] === false) {
            return;
        }

        this.loadArticle(options.active);
    },

    /**
     * 加载文章
     * @param {*} index 
     */
    loadArticle(index) {
        let options = this.data.options;
        if (options.loadding[index]) return;
        options.loadding[index] = true;
        this.setData({ options });

        let complete = (data) => {
            let options = this.data.options;
            options.loadding[index] = false;
            options.articles[index] = options.articles[index].concat(data);
            options.page[index]++;

            if (data.length == 0)
                options.page[index] = false;
            this.setData({ options });
        }
        switch (index) {
            case 0:
                this.getNewArticle(index, complete);
                break;
            case 1:
                this.getTerraceRecommendArticle(index, complete);
                break;
            case 2:
                this.getHotArticle(index, complete);
                break;
            case 3:
                this.getRecommendArticle(index, complete);
                break;
        }
    },

    onPageScroll(e) {
        if (e.scrollTop != this.data.scrollTop) {
            this.setData({ scrollTop: e.scrollTop });
        }
    },
    onShareAppMessage(e) { }
})